package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.Person;
import com.woniuxy.dudugua.mapper.OrderInfoMapper;
import com.woniuxy.dudugua.mapper.PersonMapper;
import com.woniuxy.dudugua.mapper.ProductMapper;
import com.woniuxy.dudugua.mapper.ProductSkuMapper;
import com.woniuxy.dudugua.service.MerchantsService;
import com.woniuxy.dudugua.util.Page;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.view.ReturnGoodsView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 后端商家控制层
 * 	1商品管理
 * ​商品的增删改查
 * 2、订单管理
 * 查看自家订单
 * ​发货（未发货的状态可修改成发货状态）
 * 3、门店收益查询
 *  4、售后管理
 * ​表格分页展示所有退货信息，商家审核是否通过退款，条件查询
 */
@RestController
@RequestMapping("merchant")
public class MerchantsController {
   @Resource
   private MerchantsService merchantsService;

    /**
     * 查询该商家的所有订单
     */
    @RequestMapping("selectOrderById")
    public Result selectOrderById(Integer index, Integer number, HttpSession session)throws Exception{
        //通过商家id查询,这里的商家id需要从session中获取        ~~~~~~~~~~~~~~~~~~~~
        //通过商家id，查询总条数
        Person person = (Person) session.getAttribute("person");
        Integer dataCount = merchantsService.selectCountById(person.getId());
        Integer pageCount=dataCount%number==0?dataCount/number:dataCount/number+1;
        Page page = new Page(index, dataCount, pageCount, number);
        return new Result("200","success",page,merchantsService.selectMerchantsOrder(person.getId(),index,number));
    }
    /**
     * 商家发货,商家在查询的所有订单页面，点击代发货的按钮，然后点击发货，填写快递单号和选择快递公司
     */
    @RequestMapping("sendGoods")
    public Result sendGoods(Integer orderId,Integer orderInfoId,String courierNumber,String companyCode) throws Exception{
        //传过来一个courierNumber,传过来物流公司代号，给orderinfo添加一个运单号，然后修改order表中的状态为已发货,增加一个物流信息
        merchantsService.sendGoods(orderId,orderInfoId,courierNumber,companyCode);
        return new Result("200","success",null,null);
    }
    /**
     * 商家查看所有退货申请
     */
    @RequestMapping("selectAllReturnGoods")
    public Result selectAllReturnGoods(Integer index,Integer number,HttpSession session)throws Exception{
        Person person = (Person) session.getAttribute("person");
        List<ReturnGoodsView> returnGoodsViews = merchantsService.selectAllReturnGoods(person.getId(),index,number);
        Integer dataCount = merchantsService.selectReturnCountById(person.getId());
        Integer pageCount=dataCount%number==0?dataCount/number:dataCount/number+1;
        Page page = new Page(index, dataCount, pageCount, number);
        //遍历，将时间处理后传给前端
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(ReturnGoodsView returnGoodsView:returnGoodsViews){
            Date applyTime = returnGoodsView.getApplyTime();
            String format = sdf.format(applyTime);
            returnGoodsView.setTime(format);
        }
        return new Result("200","success",page,returnGoodsViews);
    }
    /**
     * 商家通过退货申请，修改returngoods表的状态为5，然后新增一条returnprocess数据
     */
    @RequestMapping("commitReturn")
    public Result commitReturn(Integer returnGoodsId,Integer userId)throws Exception{
        merchantsService.commitReturn(returnGoodsId,userId);
        return new Result("200","success",null,null);
    }
    /**
     * 商家不通过退货申请，修改returngoods表状态为2
     */
    @RequestMapping("rejectReturn")
    public Result rejectReturn(Integer returnGoodsId)throws Exception{
        merchantsService.rejectReturn(returnGoodsId);
        return new Result("200","success",null,null);
    }
    /**
     * 商家确认收货并拒绝退款
     */
    @RequestMapping("rejectAndAcceptGoods")
    public Result rejectAndAcceptGoods(Integer returnGoodsId)throws Exception{
        merchantsService.rejectAndAcceptGoods(returnGoodsId);
        return new Result("200","success",null,null);
    }
    /**
     * 商家确认收货并同意退款
     */
    @RequestMapping("commitReturnMoney")
    public Result commitReturnMoney(Integer returnGoodsId)throws Exception{
        merchantsService.commitReturnMoney(returnGoodsId);
        return new Result("200","success",null,null);
    }
}
